package com.example.myapplication;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MusicFragment extends Fragment {

    private String[] songs = {"缘分一道桥", "最伟大的作品", "给我一首歌的时间", "江南", "美人鱼", "STAY", "光年之外"};
    private int[] imageIds = {
            R.drawable.yf,
            R.drawable.zwd,
            R.drawable.ysg,
            R.drawable.jn,
            R.drawable.mry,
            R.drawable.stay,
            R.drawable.gnzw
    };
    private int[] audioIds = {
            R.raw.gwysgdsj, // 明智之举
            R.raw.yfydq, // 安静
            R.raw.gnzw, // 小镇姑娘
            R.raw.mry, // 唯一
            R.raw.zwddzp, // 不为谁而作的歌
            R.raw.stay, // 温柔
            R.raw.jn  // 你曾是少年
    };

    private MediaPlayer mediaPlayer;
    private ImageView songImageView;
    private Button playPauseButton;
    private int currentSong = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music, container, false);
        ListView listView = view.findViewById(R.id.list_view);
        songImageView = view.findViewById(R.id.song_image);
        playPauseButton = view.findViewById(R.id.play_pause_button);

        List<Map<String, String>> data = new ArrayList<>();
        for (String song : songs) {
            Map<String, String> item = new HashMap<>();
            item.put("song_name", song);
            data.add(item);
        }

        SimpleAdapter adapter = new SimpleAdapter(getActivity(), data,
                android.R.layout.simple_list_item_1,
                new String[]{"song_name"},
                new int[]{android.R.id.text1});
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view1, position, id) -> {
            if (currentSong == position) {
                // 如果当前歌曲已经在播放，点击则停止
                stopSong();
            } else {
                // 播放新歌曲
                playOrStopSong(position);
            }
        });

        playPauseButton.setOnClickListener(v -> {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                stopSong();
            } else {
                playOrStopSong(currentSong);
            }
        });

        return view;
    }

    private void playOrStopSong(int position) {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }

        if (position >= 0 && position < audioIds.length) {
            mediaPlayer = MediaPlayer.create(getActivity(), audioIds[position]);
            mediaPlayer.start();
            songImageView.setImageResource(imageIds[position]);
            playPauseButton.setText("暂停");
            currentSong = position;

            mediaPlayer.setOnCompletionListener(mp -> {
                stopSong();
            });
        }
    }

    private void stopSong() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            playPauseButton.setText("播放");
            currentSong = -1;
            songImageView.setImageResource(0); // 清空图片
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}